package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.AddressType;
import cn.fintecher.pangolin.common.enums.PhoneType;
import cn.fintecher.pangolin.common.enums.Relationship;
import cn.fintecher.pangolin.common.enums.Source;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.*;
import cn.fintecher.pangolin.entity.mysql.AssistCase;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.entity.mysql.QAssistCase;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.dataimp.elastic.BaseCaseElastic;
import cn.fintecher.pangolin.service.dataimp.model.ConfirmModel;
import cn.fintecher.pangolin.service.dataimp.repository.AssistCaseRepository;
import cn.fintecher.pangolin.service.dataimp.repository.CaseInfoRepository;
import cn.fintecher.pangolin.service.dataimp.service.BaseService;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:18 2019/11/5
 */

@Service("confirmCaseUpdateFuture")
public class ConfirmCaseUpdateFuture {
    @Autowired
    private BaseService baseService;

    @Autowired
    private BaseCaseElastic baseCaseElastic;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private AssistCaseRepository assistCaseRepository;

    /**
     * 多线程确认案件
     */
    @Async
    public CompletableFuture<List<ConfirmModel>> processCase(List<PersonalImportModel> personalImportModels, UserModel userModel) {
        List<ConfirmModel> list = new ArrayList<>();
        List<String> caseNumbers = new ArrayList<>();
        for (PersonalImportModel personalImportModel : personalImportModels) {
            caseNumbers.add(personalImportModel.getModel().getCaseNumber());
        }
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.termsQuery("caseNumber.keyword", caseNumbers));
        Iterable<BaseCase> baseCaseIterable = baseCaseElastic.search(builder);
        Iterable<CaseInfo> caseInfoIterable = caseInfoRepository.findAll(QCaseInfo.caseInfo.caseNumber.in(caseNumbers));
        Iterable<AssistCase> assistCaseIterable = assistCaseRepository.findAll(QAssistCase.assistCase.caseNumber.in(caseNumbers));
        List<BaseCase> baseCaseList = IterableUtils.toList(baseCaseIterable);
        List<CaseInfo> caseInfoList = IterableUtils.toList(caseInfoIterable);
        List<AssistCase> assistCaseList = IterableUtils.toList(assistCaseIterable);
        for (PersonalImportModel personalImportModel : personalImportModels) {
            //初始化对象
            ConfirmModel model = new ConfirmModel(); //案件确认对象
            //检查mysql中有没有案件
            CaseInfo caseInfo = null;
            for (CaseInfo caseInfo1 : caseInfoList) {
                if (caseInfo1.getCaseNumber().equals(personalImportModel.getModel().getCaseNumber())) {
                    caseInfo = caseInfo1;
                    break;
                }
            }
            if (Objects.nonNull(caseInfo)) {
                updateCaseInfo(caseInfo, personalImportModel, userModel);
                model.setCaseInfo(caseInfo);
            }
            //检查es中有没有案件
            BaseCase baseCase = null;
            for (BaseCase baseCase1 : baseCaseList) {
                if (baseCase1.getCaseNumber().equals(personalImportModel.getModel().getCaseNumber())) {
                    baseCase = baseCase1;
                    break;
                }
            }
            if (Objects.nonNull(baseCase)) {
                updateBaseCase(baseCase, personalImportModel, userModel);
                model.setBaseCase(baseCase);
                //处理协催案件
                List<AssistCase> assistCases = new ArrayList<>();
                for (AssistCase assistCase : assistCaseList) {
                    if (baseCase.getCaseNumber().equals(assistCase.getCaseNumber())) {
                        updateAssistCase(assistCase, baseCase);
                        assistCases.add(assistCase);
                    }
                }
                model.setAssistCases(assistCases);
            }
            if (Objects.nonNull(model.getBaseCase())) {
                List<PersonalContact> personalContacts = generatePersonalContacts(personalImportModel, model.getBaseCase().getId(), userModel, personalImportModel.getModel().getCaseNumber()); //客户联系人对象集合
                List<PersonalAddress> personalAddresses = generatePersonalAddress(personalImportModel, model.getBaseCase().getId(), userModel, personalImportModel.getModel().getCaseNumber()); //客户地址信息集合
                model.setPersonalContacts(personalContacts);
                model.setPersonalAddresses(personalAddresses);
            } else if (Objects.nonNull(model.getCaseInfo())) {
                List<PersonalContact> personalContacts = generatePersonalContacts(personalImportModel, model.getCaseInfo().getId(), userModel, personalImportModel.getModel().getCaseNumber()); //客户联系人对象集合
                List<PersonalAddress> personalAddresses = generatePersonalAddress(personalImportModel, model.getCaseInfo().getId(), userModel, personalImportModel.getModel().getCaseNumber()); //客户地址信息集合
                model.setPersonalContacts(personalContacts);
                model.setPersonalAddresses(personalAddresses);
            }
            list.add(model);
            personalImportModel.setFlag(1);
        }
        return CompletableFuture.completedFuture(list);
    }

    /**
     * 更新案件信息
     */
    private void updateCaseInfo(CaseInfo caseInfo, PersonalImportModel model, UserModel userModel) {
        CaseImportModel caseImportModel = model.getModel();
        caseInfo.setCity(StringUtils.isNotBlank(caseImportModel.getCity()) ? caseImportModel.getCity() : caseInfo.getCity()); //委案城市
        caseInfo.setOverduePeriods(Objects.nonNull(caseImportModel.getOverduePeriods()) ? caseImportModel.getOverduePeriods() : caseInfo.getOverduePeriods()); //逾期期数
        caseInfo.setOverdueDays(Objects.nonNull(caseImportModel.getOverdueDays()) ? caseImportModel.getOverdueDays() : caseInfo.getOverdueDays()); //逾期天数;
        caseInfo.setAccount(StringUtils.isNotBlank(caseImportModel.getAccount()) ? caseImportModel.getAccount() : caseInfo.getAccount()); //账户号
        caseInfo.setCardNo(StringUtils.isNotBlank(caseImportModel.getCardNo()) ? caseImportModel.getCardNo() : caseInfo.getCardNo()); //卡号
        caseInfo.setCardNoType(StringUtils.isNotBlank(caseImportModel.getCardNoType()) ? caseImportModel.getCardNoType() : caseInfo.getCardNoType()); //卡类型
        caseInfo.setOpenCardDate(Objects.nonNull(caseImportModel.getOpenCardDate()) ? caseImportModel.getOpenCardDate() : caseInfo.getOpenCardDate()); //开卡日期
        caseInfo.setStopCardDate(Objects.nonNull(caseImportModel.getStopCardDate()) ? caseImportModel.getStopCardDate() : caseInfo.getStopCardDate()); //停卡日期
        caseInfo.setBillDay(Objects.nonNull(caseImportModel.getBillDay()) ? caseImportModel.getBillDay() : caseInfo.getBillDay()); //账单日
        caseInfo.setLimitAmt(Objects.nonNull(caseImportModel.getLimitAmt()) ? caseImportModel.getLimitAmt() : caseInfo.getLimitAmt()); //卡额度
        caseInfo.setBranch(StringUtils.isNotBlank(caseImportModel.getBranch()) ? caseImportModel.getBranch() : caseInfo.getBranch()); //分行
        caseInfo.setSubBranch(StringUtils.isNotBlank(caseImportModel.getSubBranch()) ? caseImportModel.getSubBranch() : caseInfo.getSubBranch()); //支行
        caseInfo.setOverdueAmtTotal(Objects.nonNull(caseImportModel.getOverdueAmtTotal()) ? caseImportModel.getOverdueAmtTotal() : caseInfo.getOverdueAmtTotal()); //委案金额
        caseInfo.setLeftAmt(Objects.nonNull(caseImportModel.getLeftAmt()) ? caseImportModel.getLeftAmt() : caseInfo.getLeftAmt()); //最新欠款金额
        caseInfo.setLatestPayAmt(Objects.nonNull(caseImportModel.getLatestPayAmt()) ? caseImportModel.getLatestPayAmt() : caseInfo.getLatestPayAmt()); //最后还款金额
        caseInfo.setLatestPayDate(Objects.nonNull(caseImportModel.getLatestPayDate()) ? caseImportModel.getLatestPayDate() : caseInfo.getLatestPayDate()); //最后还款日期
        caseInfo.setStage(StringUtils.isNotBlank(caseImportModel.getStage()) ? caseImportModel.getStage() : caseInfo.getStage()); //委案阶段
        caseInfo.setPersonalName(StringUtils.isNotBlank(model.getName()) ? model.getName() : caseInfo.getPersonalName()); //姓名
        caseInfo.setCertificateNo(StringUtils.isNotBlank(model.getCertificateNo()) ? model.getCertificateNo() : caseInfo.getCertificateNo()); //证件号
        caseInfo.setPhone(StringUtils.isNotBlank(model.getSelfPhoneNo()) ? model.getSelfPhoneNo() : caseInfo.getPhone()); //移动电话
        caseInfo.setAddress(StringUtils.isNotBlank(model.getHomeAddr()) ? model.getHomeAddr() : caseInfo.getAddress()); //住宅地址
        caseInfo.setOperator(userModel.getId());
        caseInfo.setOperatorTime(ZWDateUtil.getNowDateTime());
    }

    /**
     * 更新原始案件信息
     */
    private void updateBaseCase(BaseCase baseCase, PersonalImportModel model, UserModel userModel) {
        CaseImportModel caseImportModel = model.getModel();
        baseCase.setPersonalName(StringUtils.isNotBlank(model.getName()) ? model.getName() : baseCase.getPersonalName()); //姓名
        baseCase.setCertificateNo(StringUtils.isNotBlank(model.getCertificateNo()) ? model.getCertificateNo() : baseCase.getCertificateNo()); //证件号
        baseCase.setCertificateType(StringUtils.isNotBlank(model.getCertificateType()) ? model.getCertificateType() : baseCase.getCertificateNo()); //证件类型
        baseCase.setSex(StringUtils.isNotBlank(model.getSex()) ? model.getSex() : baseCase.getSex()); //证件类型
        baseCase.setBirthday(Objects.nonNull(model.getBirthday()) ? model.getBirthday() : baseCase.getBirthday()); //生日
        baseCase.setPhone(StringUtils.isNotBlank(model.getSelfPhoneNo()) ? model.getSelfPhoneNo() : baseCase.getPhone()); //移动电话
        baseCase.setHomePhoneNo(StringUtils.isNotBlank(model.getHomePhoneNo()) ? model.getHomePhoneNo() : baseCase.getHomePhoneNo()); //住宅电话
        baseCase.setEmployerPhoneNo(StringUtils.isNotBlank(model.getEmployerPhoneNo()) ? model.getEmployerPhoneNo() : baseCase.getEmployerPhoneNo()); //单位电话
        baseCase.setHomeAddr(StringUtils.isNotBlank(model.getHomeAddr()) ? model.getHomeAddr() : baseCase.getHomeAddr()); //住宅地址
        baseCase.setResidenceAddr(StringUtils.isNotBlank(model.getResidenceAddr()) ? model.getResidenceAddr() : baseCase.getResidenceAddr()); //户籍地址
        baseCase.setEmployerAddr(StringUtils.isNotBlank(model.getEmployerAddr()) ? model.getEmployerAddr() : baseCase.getEmployerAddr()); //单位地址
        baseCase.setEmployerName(StringUtils.isNotBlank(model.getEmployerName()) ? model.getEmployerName() : baseCase.getEmployerName()); //单位名称
        baseCase.setEmailAddr(StringUtils.isNotBlank(model.getEmailAddr()) ? model.getEmailAddr() : baseCase.getEmailAddr()); //邮箱地址
        baseCase.setBillAddr(StringUtils.isNotBlank(model.getBillAddr()) ? model.getBillAddr() : baseCase.getBillAddr()); //邮寄地址
        baseCase.setCity(StringUtils.isNotBlank(caseImportModel.getCity()) ? caseImportModel.getCity() : baseCase.getCity()); //委案城市
        baseCase.setOverduePeriods(Objects.nonNull(caseImportModel.getOverduePeriods()) ? caseImportModel.getOverduePeriods() : baseCase.getOverduePeriods()); //逾期期数
        baseCase.setOverdueDays(Objects.nonNull(caseImportModel.getOverdueDays()) ? caseImportModel.getOverdueDays() : baseCase.getOverdueDays()); //逾期天数
        baseCase.setAccount(StringUtils.isNotBlank(caseImportModel.getAccount()) ? caseImportModel.getAccount() : baseCase.getAccount()); //账户号
        baseCase.setCardNo(StringUtils.isNotBlank(caseImportModel.getCardNo()) ? caseImportModel.getCardNo() : baseCase.getCardNo()); //卡号
        baseCase.setCardNoType(StringUtils.isNotBlank(caseImportModel.getCardNoType()) ? caseImportModel.getCardNoType() : baseCase.getCardNoType()); //卡类型
        baseCase.setOpenCardDate(Objects.nonNull(caseImportModel.getOpenCardDate()) ? caseImportModel.getOpenCardDate() : baseCase.getOpenCardDate()); //开卡日期
        baseCase.setStopCardDate(Objects.nonNull(caseImportModel.getStopCardDate()) ? caseImportModel.getStopCardDate() : baseCase.getStopCardDate()); //停卡日期
        baseCase.setBillDay(Objects.nonNull(caseImportModel.getBillDay()) ? caseImportModel.getBillDay() : baseCase.getBillDay()); //账单日
        baseCase.setLimitAmt(Objects.nonNull(caseImportModel.getLimitAmt()) ? caseImportModel.getLimitAmt() : baseCase.getLimitAmt()); //卡额度
        baseCase.setExtensionAgent(StringUtils.isNotBlank(caseImportModel.getExtensionAgent()) ? caseImportModel.getExtensionAgent() : baseCase.getExtensionAgent()); //推广员
        baseCase.setExtensionInfo(StringUtils.isNotBlank(caseImportModel.getExtensionInfo()) ? caseImportModel.getExtensionInfo() : baseCase.getExtensionInfo()); //推广机构
        baseCase.setBranch(StringUtils.isNotBlank(caseImportModel.getBranch()) ? caseImportModel.getBranch() : baseCase.getBranch()); //分行
        baseCase.setRemindersDate(Objects.nonNull(caseImportModel.getRemindersDate()) ? caseImportModel.getRemindersDate() : baseCase.getRemindersDate());
        baseCase.setSubBranch(StringUtils.isNotBlank(caseImportModel.getSubBranch()) ? caseImportModel.getSubBranch() : baseCase.getSubBranch()); //支行
        baseCase.setOverdueAmtTotal(Objects.nonNull(caseImportModel.getOverdueAmtTotal()) ? caseImportModel.getOverdueAmtTotal() : baseCase.getOverdueAmtTotal()); //委案金额
        baseCase.setLeftAmt(Objects.nonNull(caseImportModel.getLeftAmt()) ? caseImportModel.getLeftAmt() : baseCase.getLeftAmt()); //最新欠款金额
        baseCase.setLatestPayAmt(Objects.nonNull(caseImportModel.getLatestPayAmt()) ? caseImportModel.getLatestPayAmt() : baseCase.getLatestPayAmt()); //最后还款金额
        baseCase.setLatestPayDate(Objects.nonNull(caseImportModel.getLatestPayDate()) ? caseImportModel.getLatestPayDate() : baseCase.getLatestPayDate()); //最后还款日期
        baseCase.setStage(StringUtils.isNotBlank(caseImportModel.getStage()) ? caseImportModel.getStage() : baseCase.getStage()); //委案阶段
        List<UnMappedValue> map = baseCase.getUnMapped();
        map.addAll(model.getUnMapped());
        baseCase.setUnMapped(map);
        baseCase.process(userModel.getRealName(), ZWDateUtil.getNowDateTime(), false);
    }


    /**
     * 更新协催案件信息
     */
    private void updateAssistCase(AssistCase assistCase, BaseCase baseCase) {
        assistCase.setPersonalName(baseCase.getPersonalName()); //客户姓名
        assistCase.setSex(baseCase.getSex()); //客户性别
        assistCase.setCertificateNo(baseCase.getCertificateNo()); //身份证号
        assistCase.setOverduePeriods(baseCase.getOverduePeriods()); //逾期期数
        assistCase.setOverdueDays(baseCase.getOverdueDays()); //逾期天数
        assistCase.setCardNo(baseCase.getCardNo()); //卡号
        assistCase.setOverdueAmtTotal(baseCase.getOverdueAmtTotal()); //委案金额
        assistCase.setLeftAmt(baseCase.getLeftAmt()); //欠款金额
        assistCase.setLatestPayDate(baseCase.getLatestPayDate()); //最后还款日期
        assistCase.setLatestPayAmt(baseCase.getLatestPayAmt()); //最后还款金额
    }

    /**
     * 生成客户联系人集合
     */
    private List<PersonalContact> generatePersonalContacts(PersonalImportModel model, String caseId, UserModel userModel, String caseNumber) {
        List<PersonalContact> list = new ArrayList<>();
        if (Objects.nonNull(model.getSelfPhoneNo())) { //本人电话不为空
            PersonalContact personalContact = setPersonalContactValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), model.getSelfPhoneNo(), PhoneType.MOBILE, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getHomePhoneNo())) { //本人住宅电话不为空
            PersonalContact personalContact = setPersonalContactValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), model.getHomePhoneNo(), PhoneType.HOME_PHONE, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getEmployerPhoneNo())) { //本人单位电话不为空
            PersonalContact personalContact = setPersonalContactValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), model.getEmployerPhoneNo(), PhoneType.OFFICE_PHONE, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo1())) { //联系人1电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation1()), caseId, model.getRemark1(), model.getPrincipalId(),
                    model.getCertificateNo1(), model.getName1(), model.getPhoneNo1(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo2())) { //联系人2电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation2()), caseId, model.getRemark2(), model.getPrincipalId(),
                    model.getCertificateNo2(), model.getName2(), model.getPhoneNo2(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo3())) { //联系人3电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation3()), caseId, model.getRemark3(), model.getPrincipalId(),
                    model.getCertificateNo3(), model.getName3(), model.getPhoneNo3(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo4())) { //联系人4电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation4()), caseId, model.getRemark4(), model.getPrincipalId(),
                    model.getCertificateNo4(), model.getName4(), model.getPhoneNo4(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo5())) { //联系人1电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation5()), caseId, model.getRemark5(), model.getPrincipalId(),
                    model.getCertificateNo5(), model.getName5(), model.getPhoneNo5(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        if (Objects.nonNull(model.getPhoneNo6())) { //联系人1电话不为空
            PersonalContact personalContact = setPersonalContactValue(baseService.relationToEnum(model.getRelation6()), caseId, model.getRemark6(), model.getPrincipalId(),
                    model.getCertificateNo6(), model.getName6(), model.getPhoneNo6(), null, userModel, caseNumber);
            list.add(personalContact);
        }
        return list;
    }

    /**
     * 客户联系人对象赋值
     */
    private PersonalContact setPersonalContactValue(cn.fintecher.pangolin.common.enums.Relationship relationship, String caseId, String remark, String principalId,
                                                    String certificateNo, String name, String phoneNo, PhoneType type, UserModel userModel, String caseNumber) {
        PersonalContact personalContact = new PersonalContact();
        personalContact.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
        personalContact.setCaseId(caseId); //案件ID
        personalContact.setCaseNumber(caseNumber); //案件编号
        personalContact.setCertificateNo(certificateNo); //证件号
        personalContact.setName(name); //姓名
        personalContact.setRelation(relationship); //关系
        personalContact.setPhoneNo(phoneNo); //电话号码
        personalContact.setType(type); //电话类型
        personalContact.setRemark(remark); //备注
        personalContact.setSource(Source.IMPORT); //来源
        personalContact.setPrincipalId(principalId); //委托方ID
        personalContact.process(userModel.getId(), ZWDateUtil.getNowDateTime(), true);
        return personalContact;
    }

    /**
     * 生成客户地址信息集合
     */
    private List<PersonalAddress> generatePersonalAddress(PersonalImportModel model, String caseId, UserModel userModel, String caseNumber) {
        List<PersonalAddress> list = new ArrayList<>();
        if (Objects.nonNull(model.getHomeAddr())) { //本人住宅地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.HOUSE_ADDR, model.getHomeAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getResidenceAddr())) { //本人户籍地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.NATIVE_ADDR, model.getResidenceAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getEmployerAddr())) { //本人单位地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.COMPANY_ADDR, model.getEmployerAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getBillAddr())) { //本人邮寄地址不为空
            PersonalAddress personalAddress = setPersonalAddressValue(cn.fintecher.pangolin.common.enums.Relationship.SELF, caseId, null, model.getPrincipalId(),
                    model.getCertificateNo(), model.getName(), AddressType.BILL_ADDR, model.getBillAddr(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail1())) { //联系地址1不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation1()), caseId, model.getRemark1(), model.getPrincipalId(),
                    model.getCertificateNo1(), model.getName1(), baseService.addressTypeToEnum(model.getAddressType1()), model.getAddressDetail1(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail2())) { //联系地址2不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation2()), caseId, model.getRemark2(), model.getPrincipalId(),
                    model.getCertificateNo2(), model.getName2(), baseService.addressTypeToEnum(model.getAddressType2()), model.getAddressDetail2(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail3())) { //联系地址3不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation3()), caseId, model.getRemark3(), model.getPrincipalId(),
                    model.getCertificateNo3(), model.getName3(), baseService.addressTypeToEnum(model.getAddressType3()), model.getAddressDetail3(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail4())) { //联系地址4不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation4()), caseId, model.getRemark4(), model.getPrincipalId(),
                    model.getCertificateNo4(), model.getName4(), baseService.addressTypeToEnum(model.getAddressType4()), model.getAddressDetail4(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail5())) { //联系地址5不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation5()), caseId, model.getRemark5(), model.getPrincipalId(),
                    model.getCertificateNo5(), model.getName5(), baseService.addressTypeToEnum(model.getAddressType5()), model.getAddressDetail5(), userModel, caseNumber);
            list.add(personalAddress);
        }
        if (Objects.nonNull(model.getAddressDetail6())) { //联系地址6不为空
            PersonalAddress personalAddress = setPersonalAddressValue(baseService.relationToEnum(model.getRelation6()), caseId, model.getRemark6(), model.getPrincipalId(),
                    model.getCertificateNo6(), model.getName1(), baseService.addressTypeToEnum(model.getAddressType6()), model.getAddressDetail6(), userModel, caseNumber);
            list.add(personalAddress);
        }
        return list;
    }

    /**
     * 客户地址对象赋值
     */
    private PersonalAddress setPersonalAddressValue(Relationship relationship, String caseId, String remark, String principalId,
                                                    String certificateNo, String name, AddressType type, String addressDetail, UserModel userModel, String caseNumber) {
        PersonalAddress personalAddress = new PersonalAddress();
        personalAddress.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
        personalAddress.setCaseId(caseId); //客户ID
        personalAddress.setCaseNumber(caseNumber); //案件编号
        personalAddress.setType(type); //地址类型
        personalAddress.setAddressDetail(addressDetail); //详细地址
        personalAddress.setCertificateNo(certificateNo); //证件号
        personalAddress.setName(name); //姓名
        personalAddress.setRelation(relationship); //关系
        personalAddress.setRemark(remark); //备注
        personalAddress.setPrincipalId(principalId); //委托方ID
        personalAddress.setSource(Source.IMPORT); //来源
        personalAddress.process(userModel.getId(), ZWDateUtil.getNowDateTime(), true);
        return personalAddress;
    }
}
